Gallery = {
	ttlPnum : 0,
	curPidx : 0,
	thumbWidth : 0,
	setting : {
		curPid : 0,
		dispMaxImgWith : 640,
		navMaxImgWith : 128,
		photos : [],
		scrollLeftLen : 300,
		navPadding : 12,
		navId : 'galleryNav',
		navImgId : 'galleryNavImgs',
		dispImgId : 'displayingPhotoImg',
		navImgPre : 'img',
		ttlPnumId : 'totalPhotoNum',
		curPidxId : 'curPhotoIndex',
		normalNavCss : '1px solid #1f1f1f',
		curNavCss : '1px solid yellow',
		preCurs : SKYLINE_DOMAIN+'/img/prior.cur',
		nextCurs : SKYLINE_DOMAIN+'/img/next.cur'
	},
	initial : function(setting) {
		$.extend(this.setting, setting);
		this.ttlPnum = setting.photos.length;
		var thumbWidth = 0;
		for ( var i = 0; i < this.ttlPnum; i++) {
			this.setting.photos[i].scrollLeft = thumbWidth;
			var navImg = $('#' + this.setting.navImgPre + i);
			thumbWidth = thumbWidth + navImg.innerWidth() + this.setting.navPadding;
			this.setting.photos[i].width = navImg.innerWidth();
		}
		this.thumbWidth = thumbWidth;
		$('#' + this.setting.navImgId).css( {
			width : thumbWidth + 'px'
		});
		for (i = 0; i < this.ttlPnum; i++) {
			if (photos[i].id == this.setting.curPid) {
				this.curPidx = i;
				break;
			}
		}
		$('#' + this.setting.ttlPnumId).html(this.ttlPnum);
		var dispImg = $('#' + this.setting.dispImgId);
		var This = this;
		dispImg.mousemove(function(e) {
			This._getPriorOrNextArrow(e)
		});
		dispImg.click(function(e) {
			This._getPriorOrNextPhoto(e)
		});
		this.getDisplayingPhoto(this.curPidx);
	},
	getDisplayingPhoto : function(idx) {
		var photo = this.setting.photos[idx];
		$('#' + this.setting.curPidxId).html(idx + 1);
		var nav = $('#' + this.setting.navId)[0];
		if (photo.scrollLeft < this.setting.scrollLeftLen) {
			nav.scrollLeft = 0;
		} else if (photo.scrollLeft > this.thumbWidth - this.setting.scrollLeftLen) {
			nav.scrollLeft = this.thumbWidth - this.setting.scrollLeftLen;
		} else {
			nav.scrollLeft = photo.scrollLeft - this.setting.scrollLeftLen + photo.width / 2;
		}

		// 在缩略图中选出该照片
		var navImg = $('#' + this.setting.navImgPre + this.curPidx);
		navImg.css( {border : this.setting.normalNavCss});
		this.curPidx = idx;
		navImg = $('#' + this.setting.navImgPre + this.curPidx);
		navImg.css( {border : this.setting.curNavCss});

		// 在displayingPhoto中输出该照片
		this._loadImage(this.setting.dispImgId, photo.file, this.setting.dispMaxImgWith);
		this.setting.afterLoad(photo);
	},
	_loadImage : function(dispId, file, maxWith) {
		var dispImg = $('#' + dispId);
		dispImg.html("正在载入");
		// displayingPhotoImg.src="";
	var imageEvent = {
		onerror : function() {
			dispImg.html("载入失败");
		},
		onload : function() {
			dispImg.html("");
			// alert(galleryDisplayingPhotoImgMaxWidth);
		if (this.width > maxWith) {
			this.width = maxWith;
		}
		dispImg[0].appendChild(img);
	}
	};

	var img = new Image();
	if (imageEvent.onerror != null) {
		img.onerror = function() {
			imageEvent.onerror.call(img);
		};
	}
	img.src = file;
	if (imageEvent.onload != null) {
		if (img.complete) {
			imageEvent.onload.call(img);
			return;
		} else {
			img.onload = function() {
				imageEvent.onload.call(img);
			};
		}
	}
},
_getPriorOrNextArrow : function(e) {
	var event = e || window.event;
	dispImg = $('#' + this.setting.dispImgId)[0];
	// alert(event.clientX);
	// var index = galleryCurPhotoIndex;
	//var screenWidth = this._getScreenWidth();
	var x = event.clientX;
	if (this._isEventXLeft(x)) {
		/*
		 * if (index == 0) { index = 0; } else { index = index - 1; }
		 */
		dispImg.style.cursor = "url('" + this.setting.preCurs + "'),auto";
	} else {
		/*
		 * if (index == galleryTotalPhotoNum - 1) { index = galleryTotalPhotoNum - 1; } else { index = index + 1; }
		 */
		dispImg.style.cursor = "url('" + this.setting.nextCurs + "'),auto";
	}
	// galleryPhotoIndex = index;
	// alert(document.body.style.cursor);
},
_getPriorOrNextPhoto : function(e) {
	var event = e || window.event;
	if (event.ctrlKey || event.shiftKey || event.altKey)
		return;
	// alert(event.clientX);
	var index = this.curPidx;
	//var screenWidth = this._getScreenWidth();
	var x = event.clientX;
	if (this._isEventXLeft(x)) {
		if (index == 0) {
			index = this.ttlPnum - 1;
		} else {
			index--;
		}
		// document.getElementById("displayingPhotoImg").style.cursor = "url('/skyline/image/albumdemo/album/prior.cur'),auto";
	} else {
		if (index == this.ttlPnum - 1) {
			index = 0;
		} else {
			index++;
		}
		// document.getElementById("displayingPhotoImg").style.cursor = "url('/skyline/image/albumdemo/album/next.cur'),auto";
	}
	// alert(index);
	this.getDisplayingPhoto(index);
},
_isEventXLeft : function(clientX) {
	var dispImg = jQuery('#' + this.setting.dispImgId)[0];
	var offsetWidth = dispImg.offsetWidth;
	var offsetLeft = dispImg.offsetLeft;
	if(clientX-offsetLeft < offsetWidth*0.5) {
		return true;
	} else {
		return false;
	}
}
/*,
_getScreenWidth : function() {
	var theWidth = 0;
	if (window.innerWidth) {
		theWidth = window.innerWidth;
	} else if (document.compatMode == 'CSS1Compat') {
		theWidth = document.documentElement.clientWidth;
	} else if (document.body) {
		theWidth = document.body.clientWidth;
	} else {
		theWidth = screen.width;
	}
	return theWidth;
}*/
}